package com.tongji.housediy.view.components
{	
	import flash.display.CapsStyle;
	import flash.display.GraphicsPath;
	import flash.display.GraphicsPathCommand;
	import flash.display.GraphicsSolidFill;
	import flash.display.GraphicsStroke;
	import flash.display.IGraphicsData;
	import flash.display.IGraphicsFill;
	import flash.display.JointStyle;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.ui.Keyboard;
	import flash.utils.ByteArray;
	
	import mx.core.UIComponent;
	
	/**
	 * 
	 * @author chenchao
	 * 
	 */
	public class FreeFormComponent extends UIComponent
	{
		private var data:Vector.<Number>;
		private var bgData:Vector.<Number>;
		
		private var graphicsData:Vector.<IGraphicsData>;
		private var bgGraphicsData:Vector.<IGraphicsData>;
		
		private var stroke:GraphicsStroke;
		private var bgStroke:IGraphicsFill;
		
		private var commands:Vector.<int>;
		private var bgCommands:Vector.<int>;
		
		private var isFirstClick:Boolean = false;
		
		public function FreeFormComponent()
		{
			data = new Vector.<Number>();
			bgData = new Vector.<Number>();
			
			var solidFill:GraphicsSolidFill = new GraphicsSolidFill(0x000000);
			stroke = new GraphicsStroke(2, false, "normal", CapsStyle.SQUARE, JointStyle.MITER, 3, solidFill);
			
			bgStroke = new GraphicsSolidFill(0xe8e8e8);
			
			commands = new Vector.<int>();
			bgCommands = new Vector.<int>();
		}
		
		public function startDrawing():void
		{
			this.parent.addEventListener(MouseEvent.CLICK, mouseDownHandler);
			this.parent.addEventListener(MouseEvent.DOUBLE_CLICK, mouseDoubleClickHandler);
			this.stage.addEventListener(KeyboardEvent.KEY_DOWN, freeFormKeyDownHandler);
			
			data.push(0, 0);
			bgData.push(0, 0, 0, 0);
			
			commands.push(GraphicsPathCommand.MOVE_TO);
			bgCommands.push(GraphicsPathCommand.MOVE_TO);
			bgCommands.push(GraphicsPathCommand.LINE_TO);
		}
		
		private function mouseDownHandler(event:MouseEvent):void
		{
			data.splice(data.length - 2, 0, this.mouseX, this.mouseY);
			bgData.splice(bgData.length - 4, 0, this.mouseX, this.mouseY);
			
			if (isFirstClick == false)
			{
				bgData[bgData.length - 2] = this.mouseX;
				bgData[bgData.length - 1] = this.mouseY;
				isFirstClick = true;
			}
			
			commands.push(GraphicsPathCommand.LINE_TO);
			bgCommands.push(GraphicsPathCommand.LINE_TO);
			
			if (!this.hasEventListener(Event.ENTER_FRAME))
				this.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
			
			event.stopImmediatePropagation();
		}
		
		private function mouseDoubleClickHandler(event:MouseEvent = null):void
		{
			this.stage.removeEventListener(KeyboardEvent.KEY_DOWN, freeFormKeyDownHandler);
			this.removeEventListener(Event.ENTER_FRAME, enterFrameHandler);
			this.parent.removeEventListener(MouseEvent.CLICK, mouseDownHandler);
			this.parent.removeEventListener(MouseEvent.DOUBLE_CLICK, mouseDoubleClickHandler);
			
			this.graphics.clear();
			
			bgData.splice(-4, 2);
			
			var offsetX:Number = bgData[0];
			var offsetY:Number = bgData[1];
			for (var i:int = 0; i < bgData.length; i++)
			{
				if (i % 2 == 0) bgData[i] -= offsetX;
				else bgData[i] -= offsetY;
			}
			
			(this.parent as GridContainer).createFreeFloor(bgData, this, offsetX, offsetY);
			
			//event.stopImmediatePropagation();
		}
		
		private function enterFrameHandler(event:Event):void
		{
			data[data.length - 2] = this.mouseX;
			data[data.length - 1] = this.mouseY;
			
			bgData[bgData.length - 4] = this.mouseX;
			bgData[bgData.length - 3] = this.mouseY;
			
			draw();
		}
		
		private function draw():void
		{
			this.graphics.clear();
			
			// bg
			var bgPath:GraphicsPath = new GraphicsPath(bgCommands, bgData);
			
			bgGraphicsData = new Vector.<IGraphicsData>();
			bgGraphicsData.push(bgStroke);
			bgGraphicsData.push(bgPath);
			
			this.graphics.drawGraphicsData(bgGraphicsData);
			
			// border
			var path:GraphicsPath = new GraphicsPath(commands, data);
			
			graphicsData = new Vector.<IGraphicsData>();
			graphicsData.push(stroke);
			graphicsData.push(path);
			
			this.graphics.drawGraphicsData(graphicsData);
		}
		
		private function freeFormKeyDownHandler(event:KeyboardEvent):void
		{
			switch (event.keyCode)
			{
				case Keyboard.ENTER:
					mouseDoubleClickHandler();
					break;
				case Keyboard.ESCAPE:
					this.stage.removeEventListener(KeyboardEvent.KEY_DOWN, freeFormKeyDownHandler);
					this.removeEventListener(Event.ENTER_FRAME, enterFrameHandler);
					this.parent.removeEventListener(MouseEvent.CLICK, mouseDownHandler);
					this.parent.removeEventListener(MouseEvent.DOUBLE_CLICK, mouseDoubleClickHandler);
					
					this.graphics.clear();
					
					(this.parent as GridContainer).removeTempFreeFormComponent(this);
					break;
			}
		}
	}
}